package com.kd.juhaowan.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.kd.juhaowan.R;
import com.luck.picture.lib.PictureSelectorPreviewFragment;
import com.luck.picture.lib.basic.IBridgeViewLifecycle;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.InjectResourceSource;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnInjectActivityPreviewListener;
import com.luck.picture.lib.interfaces.OnInjectLayoutResourceListener;

import java.util.ArrayList;


public final class GlideHelper {

    public static void loadUserAvatar(RequestManager requestManager, ImageView imageView, String url){
        final int defaultResId = R.mipmap.ic_launcher;

        requestManager.load(url).transform(new CircleCrop()).placeholder(defaultResId).error(defaultResId).into(imageView);
    }

    public static void loadUserAvatarLocal(RequestManager requestManager, ImageView imageView){
        final int defaultResId = R.mipmap.ic_launcher;
        requestManager.load(R.mipmap.ic_launcher).transform(new CircleCrop()).placeholder(defaultResId).error(defaultResId).into(imageView);
    }

    public static void loadGoodsCover(RequestManager requestManager, ImageView imageView, String url){
        if(TextUtils.isEmpty(url))return;
        if(url.startsWith("game_box/upload"))url="https://download.daim16888.com/"+url;
        requestManager.load(url).fitCenter().into(imageView);
    }



    public static void loadActivity(RequestManager requestManager, ImageView imageView, String url){
        if(TextUtils.isEmpty(url))return;
        if(url.startsWith("game_box/upload"))url="https://download.daim16888.com/"+url;
        requestManager.load(url).into(imageView);
    }





    public static void imagePreview(Activity activity, int position, ArrayList<LocalMedia> localMediaList){
        PictureSelector.create(activity)
                .openPreview()
                .setImageEngine(GlideEngine.createGlideEngine())
//                        .setVideoPlayerEngine(videoPlayerEngine)


//                        .setLanguage(language)
                        .isAutoVideoPlay(true)
//                        .isLoopAutoVideoPlay(cb_auto_video.isChecked())
//                        .isPreviewFullScreenMode(cb_preview_full.isChecked())
//                        .isVideoPauseResumePlay(cb_video_resume.isChecked())
//                        .isUseSystemVideoPlayer(isUseSystemPlayer)
//                        .setCustomLoadingListener(getCustomLoadingListener())
//                        .isPreviewZoomEffect(chooseMode != SelectMimeType.ofAudio() && cb_preview_scale.isChecked(), mRecyclerView)
                .setAttachViewLifecycle(new IBridgeViewLifecycle() {
                    @Override
                    public void onViewCreated(Fragment fragment, View view, Bundle savedInstanceState) {
//                                PictureSelectorPreviewFragment previewFragment = (PictureSelectorPreviewFragment) fragment;
//                                MediumBoldTextView tvShare = view.findViewById(R.id.tv_share);
//                                tvShare.setVisibility(View.VISIBLE)
//                                previewFragment.addAminViews(tvShare);
//                                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) tvShare.getLayoutParams();
//                                layoutParams.topMargin = cb_preview_full.isChecked() ? DensityUtil.getStatusBarHeight(getContext()) : 0;
//                                tvShare.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        PicturePreviewAdapter previewAdapter = previewFragment.getAdapter();
//                                        ViewPager2 viewPager2 = previewFragment.getViewPager2();
//                                        LocalMedia media = previewAdapter.getItem(viewPager2.getCurrentItem());
//                                        ToastUtils.showToast(fragment.getContext(), "自定义分享事件:" + viewPager2.getCurrentItem());
//                                    }
//                                });
                    }

                    @Override
                    public void onDestroy(Fragment fragment) {
//                                if (cb_preview_full.isChecked()) {
//                                    // 如果是全屏预览模式且是startFragmentPreview预览，回到自己的界面时需要恢复一下自己的沉浸式状态
//                                    // 以下提供2种解决方案:
//                                    // 1.通过ImmersiveManager.immersiveAboveAPI23重新设置一下沉浸式
//                                    int statusBarColor = ContextCompat.getColor(getContext(), R.color.ps_color_grey);
//                                    int navigationBarColor = ContextCompat.getColor(getContext(), R.color.ps_color_grey);
//                                    ImmersiveManager.immersiveAboveAPI23(MainActivity.this,
//                                            true, true,
//                                            statusBarColor, navigationBarColor, false);
//                                    // 2.让自己的titleBar的高度加上一个状态栏高度且内容PaddingTop下沉一个状态栏的高度
//                                }
                    }
                })
                .setInjectLayoutResourceListener(new OnInjectLayoutResourceListener() {
                    @Override
                    public int getLayoutResourceId(Context context, int resourceSource) {
                        return InjectResourceSource.DEFAULT_LAYOUT_RESOURCE;
                    }
                })
//                        .setExternalPreviewEventListener(new MyExternalPreviewEventListener())
                .setInjectActivityPreviewFragment(new OnInjectActivityPreviewListener() {
                    @Override
                    public PictureSelectorPreviewFragment onInjectPreviewFragment() {
                        return null;
                    }
                })
                .startActivityPreview(position, false, localMediaList);
    }
}
